/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.inventory;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import model.inventory.ItemDiskon;
import utility.NameValueList;
import utility.StringUtils;
import utility.com.TextComponentUtils;
import utility.typeSafe.DiskonType;
import utility.typeSafe.EnumUtils;

/**
 *
 * @author jay
 */
public class ItemDiskonTM extends AbstractTableModel {
    
    private List<NameValueList> jenisList = EnumUtils.enumToNameValueList(DiskonType.class);
    private List<ItemDiskon> data;

    //private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(ItemDiskonTM.class);
    
    public ItemDiskonTM(List<ItemDiskon> data) {
        this.data = data;
    }

    public ItemDiskonTM() {
        this(new ArrayList<ItemDiskon>(0));
    }

    public ItemDiskon get(int index) {
        return data.get(index);
    }

    public void add(ItemDiskon product) {
        data.add(product);
        fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
    }

    public void remove(int index) {
        data.remove(index);
        fireTableRowsDeleted(index, index);
    }

    public void set(int index, ItemDiskon product) {
        data.set(index, product);
        fireTableRowsUpdated(index, index);
    }

    public void clear() {
        data.clear();
        fireTableDataChanged();
    }

    public void add(Collection<ItemDiskon> products) {
        data.addAll(products);
        fireTableRowsInserted(getRowCount() - (products.size()), getRowCount() - 1);
    }
    
    @Override
    public boolean isCellEditable(int row, int column) {
        return true;
    }
    
    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        return 5;
    }

    @Override
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
        
        switch (columnIndex) {
            case 1: 
                BigDecimal angka = TextComponentUtils.parseNumberToBigDecimal((String)value);
                data.get(rowIndex).setDiskon(angka);
                fireTableCellUpdated(rowIndex, columnIndex);
                break;
                
            case 2: 
                Date startDate = StringUtils.stringEditorToDate(value);
                data.get(rowIndex).setBeginDate(startDate);
                fireTableCellUpdated(rowIndex, columnIndex);
                break;                            
                
            case 3: //END DATE
                Date endDate = StringUtils.stringEditorToDate(value);
                data.get(rowIndex).setEndDate(endDate);
                fireTableCellUpdated(rowIndex, columnIndex);
                break;       
        }
    }
    
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0: 
                int idx =  NameValueList.indexOf(jenisList, data.get(rowIndex).getJenis());
                if (idx > -1) { 
                    NameValueList jenis = jenisList.get(idx);
                    return jenis.getValue();
                } else { return null;}
                
            case 1: 
                String display = TextComponentUtils.formatNumber(data.get(rowIndex).getDiskon());
                return display; 
                
            case 2: 
                if (data.get(rowIndex).getBeginDate()!=null){
                    return StringUtils.formatDate(data.get(rowIndex).getBeginDate());
                } else {return null;}
                
            case 3: 
                if (data.get(rowIndex).getEndDate()!=null){
                    return StringUtils.formatDate(data.get(rowIndex).getEndDate());
                }  else {return null;}
                
            case 4: if (data.get(rowIndex).getId()!=null){data.get(rowIndex).getId().getSeq();}    
            default: return null;
        }
    }
     
//    @Override
//    public Class<?> getColumnClass(int columnIndex) {
//        switch(columnIndex){
//            case 0: return String.class;
//            case 1: return BigDecimal.class;
//            case 2: return Date.class;
//            case 3: return Date.class;
//        default: return Object.class;
//        }
//    }
    
    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0: return "JENIS DISKON";
            case 1: return "DISKON";
            case 2: return "START DATE";
            case 3: return "END DATE";
            default: return null;
        }
    }
    
}
